<template>
  <div class="page">
    <div class="content">
      <div class="top"></div>
      <div class="cont">
        <div class="from-cont">
          <form id="dt-success-cont" action="" novalidate role="form">
            <div class="dt-desc">
              <div class="dt-desc-left">
                <span class="iconfont dt-icon icon-icon_service"></span>
              </div>
              <div class="dt-desc-right">
                <p class="dt-desc-title">支付结果</p>
                <p class="dt-desc-sm">系统正在处理</p>
              </div>
            </div>
            <div class="dt-suc-title shadow-lg">
              <div class="dt-suc_cont">
                <div class="dt-form shadow-lg">
                  <div class="dt-form-row2">
                    <div class="dt-form-row-rmb">
                      您好，请稍后，订单正在处理...
                    </div>
                    <div class="dt-form-row-money"></div>
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { isWechat } from "@/utils/validate";
import qs from "qs";
export default {
  created() {
    this.init();
  },
  methods: {
    async init() {
      await this.$getUni();
      const uniEnv = this.$uni.env || { miniprogram: false };
      console.log({ uni: this.$uni }, uniEnv.miniprogram);

      // let mpToken = this.$cache.get("mpToken") || "";

      const { query } = this.$route;
      let is_payment = query.is_payment != 0;
      let { merOrderId } = query;
      let resData = "";
      if (merOrderId) {
        resData = await this.$api
          .getadparam({ order_id: merOrderId.substring(4) })
          .catch(() => ({}));
      }
      let adReturn = qs.parse(resData) ?? {};
      console.log("adReturn", uniEnv.miniprogram, adReturn);
      let paySuccess = query.status == "TRADE_SUCCESS";

      let needForm = query.form_pay == 2 && paySuccess;

      let url = `${
        needForm ? `${adReturn.page || "ads"}/form` : "pay/success"
      }`;

      const newParams = {
        ...query,
        ...adReturn
      }
      const urlParams = qs.stringify(newParams)

      let allUrl = `${location.origin}/${url}${urlParams ? "?" : ""}${
        urlParams ?? ""
      }`;

      if (!needForm && adReturn.pay_result_land && paySuccess) {
        allUrl = `${location.origin}/ad?${adReturn.pay_result_land}`;
        if (uniEnv.miniprogram) {
          this.$uni.redirectTo({
            url: "/pages/ad/ad?" + adReturn.pay_result_land,
          });
          return;
        }
      }

      console.log({ allUrl, url }, adReturn.pay_result_land);

      if (!is_payment) {
        // window.location.replace(allUrl);
      }

      if (isWechat) {
        const customerHight = document.body.scrollHight;
        let machData = {
          action: "onIframeReady",
          displayStyle: "SHOW_CUSTOM_PAGE",
          hight: customerHight,
        };
        let postData = JSON.stringify(machData);
        window.parent.postMessage(postData, "https://payapp.weixin.qq.com");
      }

      setTimeout(() => {
        if (isWechat) {
          let machData = { action: "jumpOut", jumpOutUrl: allUrl };
          let postData = JSON.stringify(machData);
          if (window.parent != window) {
            window.parent.postMessage(postData, "https://payapp.weixin.qq.com");
          } else {
            window.location.replace(allUrl);
          }
          return;
        }
        // this.$router.push({
        //   path: "/pay-success",
        //   query,
        // });
        window.location.replace(allUrl);
      }, 1800);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/assets/css/pay.scss";
</style>
